/*
 * TagsPanel.java
 *
 * Created on 17 de julio de 2007, 22:54
 */

package Interfaz;

import java.util.Vector;
import javax.swing.DefaultListModel;

/**
 *
 * @author  mariano
 */
public class TagsPanel extends javax.swing.JPanel {
    
    /** Creates new form TagsPanel */
    public TagsPanel() {
        initComponents();
        this.listTags.setModel( new DefaultListModel() );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lTag = new javax.swing.JLabel();
        tTag = new javax.swing.JTextField();
        bAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTags = new javax.swing.JList();
        bUp = new javax.swing.JButton();
        bRemove = new javax.swing.JButton();
        bDown = new javax.swing.JButton();

        lTag.setText("Tag");

        tTag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tTagActionPerformed(evt);
            }
        });

        bAdd.setText("Add");
        bAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddActionPerformed(evt);
            }
        });

        listTags.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listTags);

        bUp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/go-up.png")));
        bUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpActionPerformed(evt);
            }
        });

        bRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/list-remove.png")));
        bRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoveActionPerformed(evt);
            }
        });

        bDown.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/go-down.png")));
        bDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDownActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lTag, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tTag, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(bUp, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bRemove, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(bDown, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lTag)
                    .addComponent(tTag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bAdd))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(bUp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bRemove)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bDown))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDownActionPerformed
        downSelected();
    }//GEN-LAST:event_bDownActionPerformed

    private void bRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoveActionPerformed
        removeSelected();
    }//GEN-LAST:event_bRemoveActionPerformed

    private void bUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpActionPerformed
        upSelected();
    }//GEN-LAST:event_bUpActionPerformed

    private void bAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddActionPerformed
        addTag( this.tTag.getText() );
        this.tTag.setText( "" );
        this.tTag.requestFocus();
        
    }//GEN-LAST:event_bAddActionPerformed

    private void tTagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tTagActionPerformed
        addTag( this.tTag.getText() );
        this.tTag.setText( "" );
    }//GEN-LAST:event_tTagActionPerformed
    
    public void upSelected()
    {
        int index = this.listTags.getSelectedIndex();
        
        if( index == -1 )
            return;
        
        if( index > 0 )
        {
            String tag = (String)((DefaultListModel)this.listTags.getModel()).get( index );
            ((DefaultListModel)this.listTags.getModel()).remove( index );
            ((DefaultListModel)this.listTags.getModel()).insertElementAt( tag, index - 1  );
            this.listTags.setSelectedIndex( index - 1 );
        }
    }
    
    public void downSelected()
    {
        int index = this.listTags.getSelectedIndex();
        
        if( index == -1 )
            return;
        
        if( index + 1 < ((DefaultListModel)this.listTags.getModel()).size() )
        {
            String tag = (String)((DefaultListModel)this.listTags.getModel()).get( index );
            ((DefaultListModel)this.listTags.getModel()).remove( index );
            ((DefaultListModel)this.listTags.getModel()).insertElementAt( tag, index + 1 );
            this.listTags.setSelectedIndex( index + 1 );
        }
    }
    
    public void removeSelected()
    {
        int index = this.listTags.getSelectedIndex();
        
        if( index == -1 )
            return;
        
        ((DefaultListModel)this.listTags.getModel()).remove( index );
        
        if( ((DefaultListModel)this.listTags.getModel()).size() > 0 )
        {
            if( index < ((DefaultListModel)this.listTags.getModel()).size() )
                this.listTags.setSelectedIndex( index );
            else
                this.listTags.setSelectedIndex( index - 1 );
        }
    }
    
    public void addTag( String tag )
    {
        if( tag != "" && !((DefaultListModel)this.listTags.getModel()).contains( tag ))
            ((DefaultListModel)this.listTags.getModel()).addElement( tag );
    }
    
    public Vector<String> getTags()
    {
        Vector<String> tags = new Vector<String>();
        int size = ((DefaultListModel)this.listTags.getModel()).size();
        
        for( int i = 0 ; i < size ; i++ )
        {
            tags.add( (String)((DefaultListModel)this.listTags.getModel()).get( i ) );
        }
        
        return tags;
    }
    
    // agrega tags
    public void addTags( Vector<String> tags )
    {
        for( String tag:tags )
        {
            addTag( tag );
        }
    }
    
    // borra los que hay y lo llena de nuevo
    public void setTags( Vector<String> tags )
    {
        ((DefaultListModel)this.listTags.getModel()).clear();
        
        for( String tag:tags )
        {
            addTag( tag );
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAdd;
    private javax.swing.JButton bDown;
    private javax.swing.JButton bRemove;
    private javax.swing.JButton bUp;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lTag;
    private javax.swing.JList listTags;
    private javax.swing.JTextField tTag;
    // End of variables declaration//GEN-END:variables
    
}
